package cn.han.encoding;

import java.io.*;

public class TestEncode {
    public static void main(String[] args) {
        method1();//用来测试OutputStreamWriter
        method2();//用来测试InputStreamReader
    }

    private static void method2() {
        InputStreamReader in = null;
        try {
            in = new InputStreamReader(new FileInputStream("1.txt"), "utf-8");
//            System.out.println(in.read());//read返回值类型是int，打印的是编码
            char[] ch = new char[8192];
            int len = in.read(ch);//将字符读入数组，返回读取到的字符的个数，
            // len保存的是读取到的字符的个数
            System.out.println(new String(ch,0,len));
            System.out.println("chenggong");
        } catch (Exception e) {
            System.out.println("shibai");
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    private static void method1() {
        //1.创建一个在本方法中都生效的局部变量
        OutputStreamWriter out = null;
        try{
            out = new OutputStreamWriter(new FileOutputStream("1.txt"),"utf-8");
//            out = new OutputStreamWriter(new FileOutputStream("1.txt"),"gbk");
//            out = new OutputStreamWriter(new FileOutputStream("1.txt"),"iso-8859-1");
            out.write("海绵宝宝与派大星");
            System.out.println("执行成功");
        }catch (Exception e){
            System.out.println("执行失败");
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
